package com.ygo.feihua.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import cn.bmob.v3.BmobUser;
import com.ygo.feihua.BmobTable.MyUser;
import com.ygo.feihua.MainService;
import java.util.ArrayList;
import java.util.List;
import com.ygo.feihua.adapter.AdapterTextview;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.EditText;
import cn.bmob.v3.BmobQuery;
import com.ygo.feihua.BmobTable.Tiezi;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.exception.BmobException;
import com.ygo.feihua.BmobTable.ZiXun;
import cn.bmob.v3.listener.SaveListener;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;
import android.content.*;
import com.msk86.ygoroid.newutils.*;
import com.ygo.feihua.*;
import com.xiaomi.mistatistic.sdk.*;

public class FragmentShezhi extends Fragment
{

	
    ListView sz_list;
	Gongju gj;
    int qx=0;
    List<String> data =new ArrayList<String>();
    AdapterTextview adpt;
    DialogUtils du;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		// TODO: Implement this method
		
		View v=inflater.inflate(R.layout.fragment_shezhi,null);
		
		
        sz_list=(ListView) v.findViewById(R.id.sz_list);
		gj =Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		UserManagement um=UserManagement.getDx();
		if(um.getUser()!=null){
		qx=um.getUser().getUser_qx();
		}
        
        data.add("战队提示列表");
        data.add("强制转屏");
        data.add("各种开关");
        data.add("路径设置");
		data.add("决斗辅助信息");
        switch(qx){
            case 1:
				data.add("推送设置");
				data.add("资讯管理");
                data.add("公告管理");
                data.add("广告管理");
                break;
        }
        
        adpt=new AdapterTextview(getActivity(),data);
        sz_list.setAdapter(adpt);
        
       final int gs=data.size();
        sz_list.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
                {
                    switch(p3){
                        case 0:
                            startActivity(new Intent(getActivity(),NewZtliebiao.class));
                            break;
                        case 1:
                            String zt="关";
                            if(!gj.isservicerun("com.ygo.feihua.MainService")){
                                zt="关";

                            }
                            else{
                                zt="开";
                                //getActivity().stopService(new Intent(getActivity(), MainService.class));
                            }

                            View[] v1=du.dialogt(true,"状态:"+zt,"开启本功能后可强制横屏或竖屏\n可用于ygomobile决斗界面无法旋转的情况等");
                            Button b1=(Button) v1[0];
                            Button b2=(Button) v1[1];
                            b1.setText("关闭");
                            b2.setText("开启");
                            b2.setOnClickListener(new OnClickListener(){

                                    @Override
                                    public void onClick(View p1)
                                    {
                                        getActivity().startService(new Intent(getActivity(), MainService.class));
                                        du.dis();
                                        // TODO: Implement this method
                                    }
                                });
                            b1.setOnClickListener(new OnClickListener(){

                                    @Override
                                    public void onClick(View p1)
                                    {
                                        getActivity().stopService(new Intent(getActivity(), MainService.class));
                                        du.dis();
                                        // TODO: Implement this method
                                    }
                                });
                            break;
                        case 2:
                            startActivity(new Intent(getActivity(),ShezhiKaiguan.class));
                            break;
                        case 3:
							startActivity(new Intent(getActivity(),ShezhiEzmanagement.class));
							break;
						case 4:
							final String[] ss=new String[]{"用户名","ip地址","端口"};
							ListView ll=(ListView) du.dialogl(true,"决斗辅助信息配置",ss);
							ll.setOnItemClickListener(new OnItemClickListener(){

									@Override
									public void onItemClick(AdapterView<?> p1, View p2, final int position, long p4)
									{
										String ts="";
										switch(position){
											case 0:
												ts=SharedPreferenceUtil.getDuelName();
										break;
											case 1:
												ts=SharedPreferenceUtil.getDuelIp();
												break;
											case 2:
												ts=SharedPreferenceUtil.getDuelPort()+"";
												break;
										}
										
										View[] v=du.dialoge(true,ss[position],"");
										final EditText ed=(EditText) v[0];
										ed.setText(ts);
										Button b1=(Button) v[1];
										
										b1.setOnClickListener(new OnClickListener(){

												@Override
												public void onClick(View p1)
												{
													String message=ed.getText().toString().trim();
													if(message.equals("")){
														gj.show("信息不能为空");
													}else{
														
														du.dis();
														SharedPreferences sh=SharedPreferenceUtil.getShareDuelInfo();
														switch(position){
															case 0:
																sh.edit().putString("name",message).commit();
																break;
															case 1:
																sh.edit().putString("ip",message).commit();
																break;
															case 2:
																sh.edit().putInt("port",Integer.parseInt( message)).commit();
																break;
														}
														gj.show("修改成功,请重启"+Utils.s(R.string.app_name)+"生效");
													}
													// TODO: Implement this method
												}
											});
										// TODO: Implement this method
									}
								});
							break;
                    }
                    
                    switch(qx){
                        case 1:
                            if(p3==gs-1){
                                startActivity(new Intent(getActivity(),ShezhiGg1gl.class));
                             }else if(p3==gs-2){
                                 startActivity(new Intent(getActivity(),ShezhiGggl.class));
                             }else if(p3==gs-3){
								 addzx();
							 }else if(p3==gs-4){
								 startActivity(new Intent(getActivity(),PushManagement.class));
							 }
							 
                            break;
                        
                    }
                    // TODO: Implement this method
                }
            });
		return v;
	}
	

	@Override
	public void onResume(){
		// TODO: Implement this method
		super.onResume();
		MiStatInterface.recordPageStart(getActivity(), "设置");
	}

	@Override
	public void onPause(){
		// TODO: Implement this method
		super.onPause();
		MiStatInterface.recordPageEnd();
	}
	
	
	private void  addzx(){
		View[] v=du.dialoge(false,"","请输入资讯标题");
		final EditText ed=(EditText) v[0];
		Button b1=(Button) v[1];
		b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String bt=ed.getText().toString().trim();
					
					if(bt.equals("")){
						gj.show("标题不能为空");
					}else{
						du.dis();
						xzimage(p1,bt);
						
					}
					// TODO: Implement this method
				}
			});
		
	}
	
	private void xzimage(final View v,final String bt){
		BmobQuery<Tiezi> bq=new BmobQuery<Tiezi>();
		bq.order("-createdAt");
		bq.findObjects(new FindListener<Tiezi>(){

				@Override
				public void done(final List<Tiezi> tie, BmobException p2)
				{
					if(p2==null){
						String[] ss=new String[tie.size()];
						for(int i=0;i<tie.size();i++){
							ss[i]=tie.get(i).gettitle();
							
						}
						ListView list=(ListView) du.dialogl(true,"请选择资讯帖子",ss);
						
						list.setOnItemClickListener(new OnItemClickListener(){

								@Override
								public void onItemClick(AdapterView<?> p1, final View p2v, int p3, long p4)
								{
									du.dis();
									du.dialogj1(false,"","添加中,请稍等");
									
									ZiXun zxx=new ZiXun();
									zxx.setZs_title(bt);
									zxx.setZs_message(tie.get(p3));
									zxx.save(new SaveListener<String>(){

											@Override
											public void done(String p1, BmobException p2)
											{
												if(p2==null){
													du.dis();
													gj.show("添加成功");
													du.dis();
												}else{
													du.dis();
													gj.toastBmobException(v,"添加失败",p2);
													//gj.show("添加失败,原因为"+p2);
												}
												// TODO: Implement this method
											}
										});
									// TODO: Implement this method
								}
							});
						
						
					}else{
						gj.toastBmobException(v,"查询图片失败",p2);
					}
					// TODO: Implement this method
				}
			});
		
	}
	
}
